<template>
  <div class="pricing-management">
    <div class="header">
      <h2 class="header-title">判单参数设置</h2>
      <el-button type="primary" @click="openDialog" style="float: right">修改</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="描述">
        <template v-slot:default="scope">
          <el-tag>{{ scope.row.label }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="参数">
        <template v-slot:default="scope">
          {{ scope.row.value }}  {{scope.row.unit}}
        </template>
      </el-table-column>
      <el-table-column label="说明">
        <template v-slot:default="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑司机费用" :visible.sync="dialogVisible">
      <el-form :model="formData">
        <el-form-item label="网约车半径" label-width="120px">
          <el-input v-model="formData.netMatchRadius" type="number" step="0.01"/>
        </el-form-item>
        <el-form-item label="拼车搜索半径" label-width="120px">
          <el-input v-model="formData.carpoolingMatchRadius" type="number" step="0.01"/>
        </el-form-item>
        <el-form-item label="每单推送次数" label-width="120px">
          <el-input v-model="formData.maxPush" type="number" step="0.01"/>
        </el-form-item>
        <el-form-item label="推送间隔" label-width="120px">
          <el-input v-model="formData.pushBuf" type="number" step="0.01"/>
        </el-form-item>
        <el-form-item label="司机取消次数" label-width="120px">
          <el-input v-model="formData.driverMaxCancel" type="number" step="0.01"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveChanges">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, updateConfig } from '@/api/operate/config'

export default {
  data() {
    return {
      dialogVisible: false,
      formData: {
        netMatchRadius: 0,
        carpoolingMatchRadius: 0,
        maxPush: 0,
        pushBuf: 0,
        driverMaxCancel: 0
      },
      resData: {}
    }
  },
  computed: {
    tableData() {
      return [
        {
          label: '网约车半径',
          value: this.formData.netMatchRadius,
          description: '确定在哪个范围内的司机将接收到订单请求，确保乘客能够快速上车。',
          unit: 'Km'
        },
        {
          label: '拼车搜索半径',
          value: this.formData.carpoolingMatchRadius,
          description: '在此半径范围内搜索是否有可以拼车的其他乘客，优化资源利用率和成本。',
          unit: 'Km'
        },
        {
          label: '每单推送次数',
          value: this.formData.maxPush,
          description: '在订单发出后，可以多次推送给范围内的司机。若到达最大次数仍无人接单，则需要采取其他措施（如扩大搜索范围）。',
          unit: '次'
        },
        {
          label: '推送间隔',
          value: this.formData.pushBuf,
          description: '两次订单提醒之间保持一定的时间间隔，避免过于频繁打扰司机，影响用户体验。',
          unit: '秒'
        },
        {
          label: '司机取消次数',
          value: this.formData.driverMaxCancel,
          description: '司机每天接单后最大取消次数（超出次数则会影响司机接单）',
          unit: '次'
        }
      ]
    },
  },
  methods: {
    openDialog() {
      this.dialogVisible = true
    },
    saveChanges() {
      // 处理保存逻辑，例如调用API保存更改的数据
      updateConfig(this.formData).then(res => {
        this.$modal.success(res.msg)
      })
      console.log('保存数据:', this.formData)
      this.dialogVisible = false
    },
    getDriverPayRule(){
      //初始化表单数据
      listConfig().then(res => {
        this.formData = res
        console.log(this.formData)
      })
    }
  },
  created() {
    this.getDriverPayRule()
  }
}
</script>

<style scoped>
.pricing-management {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
}

.header-title {
  font-size: 18px;
  display: inline;
}
</style>
